package com.xxxx.comment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.comment.constants.RedisConstants;
import com.xxxx.comment.constants.httpcode.HttpConstants;
import com.xxxx.comment.entity.Follow;
import com.xxxx.comment.entity.User;
import com.xxxx.comment.entity.dto.Result;
import com.xxxx.comment.entity.dto.UserDTO;
import com.xxxx.comment.mapper.FollowMapper;
import com.xxxx.comment.service.IFollowService;
import com.xxxx.comment.service.IUserService;
import com.xxxx.comment.utils.UserHolder;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private FollowMapper followMapper;

    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;

    @Resource
    private IUserService userService;

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 1、获取信息
        Long userId = UserHolder.getUser().getId();
        if (userId.equals(followUserId))
            return Result.fail(HttpConstants.NOT_FOLLOW_YOURSELF);
        String key = RedisConstants.FOLLOW_KEY + userId;
        // 2、提前到数据库中获取关注数据，避免 “重复关注” 以及 “取关未关注的人”这两个问题
        Follow follow = followMapper.getFollow(followUserId, userId);
        // 3、判断是关注还是取关
        if (isFollow) {
            if (follow != null)
                return Result.ok(HttpConstants.HAD_FOLLOWED);
            // 4、关注，新增一条记录到数据库中
            follow = new Follow();
            follow.setFollowUserId(followUserId);
            follow.setUserId(userId);
            if (save(follow)) {
                // 关注成功，将该请求用户的关注信息存入redis
                redisTemplate.opsForSet().add(key, followUserId.toString());
                return Result.ok(HttpConstants.FOLLOW_SUCCESS);
            }
        } else {
            // 5、取消关注
            if (follow == null)
                return Result.fail(HttpConstants.NOT_FOLLOWED);
            if (followMapper.unFollow(follow.getId())) {
                // 取关成功，从redis中删除该请求用户的该条关注信息
                redisTemplate.opsForSet().remove(key, followUserId.toString());
                return Result.ok(HttpConstants.UNFOLLOW_SUCCESS);
            }
        }
        return Result.fail(HttpConstants.SYSTEM_ERROR);
    }

    @Override
    public Result isFollow(Long followUserId) {
        return Result.ok(followMapper.isFollow(UserHolder.getUser().getId(), followUserId));
    }

    @Override
    public Result commonFollow(Long userId) {
        // 1、获取当前登录用户
        UserDTO user = UserHolder.getUser();
        String currentUserKey = RedisConstants.FOLLOW_KEY + user.getId();
        String targetUserKey = RedisConstants.FOLLOW_KEY + userId;
        // 2、通过redis的求交集API获取两个用户的共同关注
        Set<String> commonFollow = redisTemplate.opsForSet().intersect(currentUserKey, targetUserKey);
        if (commonFollow == null || commonFollow.isEmpty()) // 没有交集，返回空列表
            return Result.ok(Collections.emptyList());
        // 3、解析ID
        List<User> users = userService.listByIds(commonFollow);

        return Result.ok(users);
    }

}
